#include "net_pre.h"
#include "connector.h"
#include "common.h"

namespace Net
{
	Connector::Connector(boost::asio::io_service& io_service)
		: INetSession(io_service)
	{

	}

	Connector::~Connector()
	{

	}

	void Connector::Connect(std::string ip, ushort port)
	{
		boost::asio::ip::tcp::endpoint endpoint(boost::asio::ip::address::from_string(ip), port);
		GetSocket().connect(endpoint);
	}

	void Connector::Connect(ulong /*ip*/, ushort /*port*/)
	{

	}

	void Connector::AsyncConnect(std::string ip, ushort port)
	{
		boost::asio::ip::tcp::endpoint endpoint(boost::asio::ip::address::from_string(ip), port);
		GetSocket().async_connect(endpoint, boost::bind(&Connector::ConnectHandle, this, boost::asio::placeholders::error));
	}

	void Connector::AsyncConnect(ulong /*ip*/, ushort /*port*/)
	{

	}

	void Connector::ConnectHandle(boost::system::error_code ec)
	{
		if (ec)
		{
			if (connect_failed_callback_)
				connect_failed_callback_();
		}
		else
		{
			if (connect_callback_)
				connect_callback_();
		}
	}
}